JavaScript provides several methods to work with numbers. These methods can be used to convert, format, and manipulate numbers.
The toString() method converts a number to a string.
let num = 123;
console.log(num.toString()); // Outputs: "123"
The toExponential() method converts a number to exponential notation.
let num = 9.656;
console.log(num.toExponential(2)); // Outputs: "9.66e+0"
The toFixed() method formats a number using fixed-point notation.
let num = 9.656;
console.log(num.toFixed(2)); // Outputs: "9.66"
The toPrecision() method formats a number to a specified length.
let num = 9.656;
console.log(num.toPrecision(2)); // Outputs: "9.7"
The valueOf() method returns the primitive value of a number.
let num = 123;
console.log(num.valueOf()); // Outputs: 123
The Number() function converts a value to a number.
console.log(Number("123")); // Outputs: 123
console.log(Number("123.45")); // Outputs: 123.45
The parseFloat() function parses a string and returns a floating point number.
console.log(parseFloat("123.45")); // Outputs: 123.45
The parseInt() function parses a string and returns an integer.
console.log(parseInt("123.45")); // Outputs: 123